package com.gmlive.gradle.versionlint.repositories

import com.gmlive.gradle.versionlint.library.Library
import com.gmlive.gradle.versionlint.library.isInkeLib
import okhttp3.*
import java.io.IOException


internal abstract class MavenRemoteRepository(private val client: OkHttpClient) {

    fun findNewVersionFromLibrary(library: Library, result: (result: RepositoryResult) -> Unit) {
        client.newCall(createQueryFromLibrary(library)).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                result.invoke(RepositoryResult.ArtifactNotFound)
            }

            override fun onResponse(call: Call, response: Response) {
                val resultString = response.body()?.string() ?: ""
                result.invoke(getNewVersionOrNull(library, resultString))
            }

        })
    }

    protected abstract fun getNewVersionOrNull(
        actualLibrary: Library,
        responseBody: String
    ): RepositoryResult

    protected abstract fun createQueryFromLibrary(library: Library): Request
}